#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
const int maxm = 1e6+10;
long long s[maxn],last[maxm],dp[maxn];

long long solve2(int l,int r)
{
    long long ans = 0;
    for(int i = l+1;i <= r;i++)
    {
        if(s[i] == s[i-1]) ans += s[i];
    }
    return ans;
}
long long solve(int l,int r)
{

    memset(last,0,sizeof(last));
    memset(dp,0,sizeof(dp));

    last[s[l]] = l;
    for(int i = l+1;i <= r;i++)
    {
        dp[i] = dp[i-1];
        if(last[s[i]] != 0)
        {
            long long k = solve2(last[s[i]]+1,i-1);
            dp[i] = max(dp[last[s[i]]+1]+s[i]+k,dp[i]);
        }
        last[s[i]] = i;
    }

    //if(s[1] == 1) cout<<dp[4];
    return dp[r];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);

    int T;cin>>T;
    while(T--)
    {
        int n;cin>>n;
        for(int i = 1;i <= n;i++) cin>>s[i];

        cout<<solve(1,n)<<'\n';
    }

    return 0;
}
